package psn.qiuyiping.autocad.dxffile;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.util.Map;

public class DefaultDxf0 extends DxfBlockReader {
    protected Map<Integer, Member> fields;

    public DefaultDxf0() {
        super();
        fields = initFields();
    }

    public void readDxf(DxfProcessContext context, DxfData item) {
        if (item.code == 0) {
            afterBlockEnd(context);
            context.processDxf0(item);
            return;
        } else {
            readDxfBody(context, item);
        }
    }

    public void readDxfBody(DxfProcessContext context, DxfData<Object> item) {
        Member member = fields.get(item.code);
        if (member != null) {
            try {
                if (member instanceof Field) {
                    ((Field) member).set(this, item.getVal());
                } else {
                    ((Method) member).invoke(this, context, item);
                }
            } catch (IllegalArgumentException | IllegalAccessException
                    | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    
    public void afterBlockEnd(DxfProcessContext context) {
    }

}
